@{
    var currentPage = ViewBag.CurrentPage ?? 1;
    var totalPages = ViewBag.TotalPages ?? 1;
    var totalCount = ViewBag.TotalCount ?? 0;
    var pageSize = ViewBag.PageSize ?? 10;
    var queryParams = ViewContext.HttpContext.Request.Query.ToDictionary(x => x.Key, x => x.Value.ToString());
    
    string BuildUrl(int page)
    {
        queryParams["page"] = page.ToString();
        var queryString = string.Join("&", queryParams.Select(x => $"{x.Key}={x.Value}"));
        return $"?{queryString}";
    }
}

@if (totalPages > 1)
{
    <div class="card-footer">
        <div class="d-flex justify-content-between align-items-center">
            <div class="pagination-info">
                显示 @((currentPage - 1) * pageSize + 1) - @(Math.Min(currentPage * pageSize, totalCount)) 条，共 @totalCount 条
            </div>
            <nav>
                <ul class="pagination pagination-ace mb-0">
                    @if (currentPage > 1)
                    {
                        <li class="page-item">
                            <a class="page-link" href="@BuildUrl(1)">
                                <i class="bi bi-chevron-double-left"></i>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="@BuildUrl(currentPage - 1)">
                                <i class="bi bi-chevron-left"></i>
                            </a>
                        </li>
                    }
                    else
                    {
                        <li class="page-item disabled">
                            <span class="page-link">
                                <i class="bi bi-chevron-double-left"></i>
                            </span>
                        </li>
                        <li class="page-item disabled">
                            <span class="page-link">
                                <i class="bi bi-chevron-left"></i>
                            </span>
                        </li>
                    }
                    
                    @{
                        var startPage = Math.Max(1, currentPage - 2);
                        var endPage = Math.Min(totalPages, currentPage + 2);
                        
                        if (endPage - startPage < 4)
                        {
                            if (startPage == 1)
                            {
                                endPage = Math.Min(totalPages, startPage + 4);
                            }
                            else if (endPage == totalPages)
                            {
                                startPage = Math.Max(1, endPage - 4);
                            }
                        }
                    }
                    
                    @if (startPage > 1)
                    {
                        <li class="page-item">
                            <a class="page-link" href="@BuildUrl(1)">1</a>
                        </li>
                        @if (startPage > 2)
                        {
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        }
                    }
                    
                    @for (int i = startPage; i <= endPage; i++)
                    {
                        <li class="page-item @(i == currentPage ? "active" : "")">
                            <a class="page-link" href="@BuildUrl(i)">@i</a>
                        </li>
                    }
                    
                    @if (endPage < totalPages)
                    {
                        @if (endPage < totalPages - 1)
                        {
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        }
                        <li class="page-item">
                            <a class="page-link" href="@BuildUrl(totalPages)">@totalPages</a>
                        </li>
                    }
                    
                    @if (currentPage < totalPages)
                    {
                        <li class="page-item">
                            <a class="page-link" href="@BuildUrl(currentPage + 1)">
                                <i class="bi bi-chevron-right"></i>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="@BuildUrl(totalPages)">
                                <i class="bi bi-chevron-double-right"></i>
                            </a>
                        </li>
                    }
                    else
                    {
                        <li class="page-item disabled">
                            <span class="page-link">
                                <i class="bi bi-chevron-right"></i>
                            </span>
                        </li>
                        <li class="page-item disabled">
                            <span class="page-link">
                                <i class="bi bi-chevron-double-right"></i>
                            </span>
                        </li>
                    }
                </ul>
            </nav>
        </div>
    </div>
}